#include "include.h"
#include "func.h"
#include "func_music.h"
#include "func_exspiflash_music.h"
#include "api_spiflash_ex.h"

#if 0//(EX_SPIFLASH_SUPPORT)
#define MAX_PRV_SONG 5
#define MAX_CUR_SONG 2
#define MAX_AFTER_SONG 5
#define MAX_MICOFF_TIMEOUT_TIME 30
#define MAX_MICON_TIMEOUT_TIME 5*60

exspifalsh_msc_t exspi_msc AT(.buf.func.music);
u16 play_time=0;
u32 song_ticks = 0;
u8 bakup_light_mode=0;
AT(.text.func.music.table)
const u8 max_song_table[]=
{
	MAX_PRV_SONG,MAX_CUR_SONG,MAX_AFTER_SONG
};
AT(.text.func.music.table)
const u8 song_mode_table[3][5]=
{
	{1,2,3,4,5},
	{6,7,7,7,7},
	{10,11,12,24,13},
};
AT(.text.func.music.table)
const u8 light_mode_table[3][5]=
{
	{LIGHT_SEQUENCE_3A,LIGHT_SEQUENCE_3B,LIGHT_SEQUENCE_3A,LIGHT_SEQUENCE_3A,LIGHT_SEQUENCE_3B},
	{LIGHT_SEQUENCE_5,LIGHT_SEQUENCE_5,LIGHT_SEQUENCE_14,LIGHT_SEQUENCE_14,LIGHT_SEQUENCE_14},
	{LIGHT_SEQUENCE_3A,LIGHT_SEQUENCE_3A,LIGHT_SEQUENCE_3A,LIGHT_SEQUENCE_3B,LIGHT_SEQUENCE_3A},
};

AT(.text.func.music.table)
const u16 delay_info_table[3][5]=
{
	{500,500,500,500,500},
	{1300,500,0,0,0},
	{0,0,500,0,0},
};
AT(.text.func.music.table)
const u8 int_light_table[]=
{
	LIGHT_SEQUENCE_14,LIGHT_SEQUENCE_14
};
AT(.text.func.music.table)
const u8 int_song_table[]=
{
	8,9
};
AT(.text.func.music.table)
const u8 mic_light_table[]=
{
	LIGHT_SEQUENCE_14,LIGHT_SEQUENCE_1
};
AT(.text.func.music.table)
const u8 mic_ctl_table[]=
{
	15,14
};

AT(.text.func.music.table)
const song_time_play_t song1_time[]=
{
	{0,LIGHT_SEQUENCE_7},		//1
	{797,LIGHT_SEQUENCE_8},
	{1622,LIGHT_SEQUENCE_4B},
	{2212,LIGHT_SEQUENCE_4C},
	{2435,LIGHT_SEQUENCE_4A},
	{3036,LIGHT_SEQUENCE_4C},
	{3250,LIGHT_SEQUENCE_4B},
	{4065,LIGHT_SEQUENCE_4A},
	{4675,LIGHT_SEQUENCE_4C},
	{4904,LIGHT_SEQUENCE_4D},//10
	{6401,LIGHT_SEQUENCE_16A},
	{6578,LIGHT_SEQUENCE_16},
	{6654,LIGHT_SEQUENCE_4D},
	{6907,LIGHT_SEQUENCE_16},
	{6987,LIGHT_SEQUENCE_16A},
	{7066,LIGHT_SEQUENCE_4D},
	{8069,LIGHT_SEQUENCE_1},
	{8580,LIGHT_SEQUENCE_4A},
	{9131,LIGHT_SEQUENCE_4C},
	{9345,LIGHT_SEQUENCE_4B},//20
	{9947,LIGHT_SEQUENCE_4C},
	{10165,LIGHT_SEQUENCE_4A},
	{10977,LIGHT_SEQUENCE_4B},
	{11611,LIGHT_SEQUENCE_1},
	{11870,LIGHT_SEQUENCE_4D},
	{13358,LIGHT_SEQUENCE_16A},
	{13489,LIGHT_SEQUENCE_16},
	{13573,LIGHT_SEQUENCE_16A},
	{13645,LIGHT_SEQUENCE_4D},
	{13830,LIGHT_SEQUENCE_16},//30
	{13902,LIGHT_SEQUENCE_16A},
	{13977,LIGHT_SEQUENCE_17},
	{14110,LIGHT_SEQUENCE_4D},
	{15104,LIGHT_SEQUENCE_4A},
	{15253,LIGHT_SEQUENCE_4B},
	{15530,LIGHT_SEQUENCE_4A},
	{15683,LIGHT_SEQUENCE_4D},
	{15855,LIGHT_SEQUENCE_4A},
	{16082,LIGHT_SEQUENCE_4B},//40
	{16340,LIGHT_SEQUENCE_4B},
	{16488,LIGHT_SEQUENCE_4D},
	{16665,LIGHT_SEQUENCE_4B},
	{17491,LIGHT_SEQUENCE_4A},
	{18066,LIGHT_SEQUENCE_1},
	{18519,LIGHT_SEQUENCE_4D},
	{20131,LIGHT_SEQUENCE_4A},
	{20213,LIGHT_SEQUENCE_4B},
	{20282,LIGHT_SEQUENCE_4D},
	{20535,LIGHT_SEQUENCE_4B},	//50
	{20613,LIGHT_SEQUENCE_4A},
	{20695,LIGHT_SEQUENCE_17},
	{20946,LIGHT_SEQUENCE_4D},
	{21692,LIGHT_SEQUENCE_1},
	{22118,LIGHT_SEQUENCE_17},
	{22202,LIGHT_SEQUENCE_18}, //56
};
AT(.text.func.music.table)
const song_time_play_t song2_time[]=
{
	{0,LIGHT_SEQUENCE_1},		//1
	{149,LIGHT_SEQUENCE_10},
	{1913,LIGHT_SEQUENCE_4A},
	{2339,LIGHT_SEQUENCE_1},
	{2582,LIGHT_SEQUENCE_17},
	{2685,LIGHT_SEQUENCE_4A},
	{3425,LIGHT_SEQUENCE_17},
	{3516,LIGHT_SEQUENCE_4A},
	{4654,LIGHT_SEQUENCE_1},
	{5030,LIGHT_SEQUENCE_10},//10
	{6452,LIGHT_SEQUENCE_17},
	{6527,LIGHT_SEQUENCE_13},
	{8073,LIGHT_SEQUENCE_17},
};

AT(.text.func.music.table)
const u8 sfx_song_table[]=
{
	16,17,18,19,20,21,9,8,22,23
};

AT(.text.func.music.table)
const u8 sfx_light_mode_table[]=
{
	LIGHT_SEQUENCE_7,
	LIGHT_SEQUENCE_8,
	LIGHT_SEQUENCE_9,
	LIGHT_SEQUENCE_10,
	LIGHT_SEQUENCE_11,
	LIGHT_SEQUENCE_12,
	LIGHT_SEQUENCE_13,
	LIGHT_SEQUENCE_2,
	LIGHT_SEQUENCE_20,
};


AT(.text.func.music.table)
const u8 song_table[3]={2,1,3};
u32 spi_mp3_offset_ending = 512*0;      //开头偏移多少byte
u32 spi_mp3_offset_beginning = 0;       //结尾偏移多少byte
static u32 ticks_1 = 0;
u8 tick_start=0;
bool spi_mp3_loopback_en(void)
{
    return SPLFLASH_MP3_LOOPBACK_EN;
}
void spi_mp3_loopback_start(void)
{
    //static u32 spi_loop_cnt = 0;
    //printf("loop times: %d\n", ++spi_loop_cnt);
}

bool exspiflash_init(void)
{
    printf("exspiflash_init\n");
    spiflash1_init();
    if (!is_exspiflash_online()) {
        return false;
    }
#if  SPIFALSH_MUSIC_BIN_WRITE_TEST    //默认的FLASH_MUSIC.BIN写入SPIFLASH, 可以用于测试外接SPIFALSH播放MP3.
    write_music_bin_to_spiflash();
#endif

#if KARAOK_REC_EN
    cycrec_fs_init(SPIFLASH_REC_BEGIN_ADDR, SPIFLASH_REC_END_ADDR, spiflash1_read, spiflash1_write, spiflash1_erase);
#endif
    return true;
}
AT(.text.func.music)
void set_song_play_num(void)
{
	u8 song_index=exspi_msc.song_info[exspi_msc.song_mode].song_index;
	u8 set_light_mode;
	if(exspi_msc.mic_play_flag)
	{
		exspi_msc.cur_num=mic_ctl_table[sys_cb.mic_onoff];
		set_light_mode=mic_light_table[sys_cb.mic_onoff];
		printf("mic cur_num:%d\n",exspi_msc.cur_num);
	}
	else if(exspi_msc.sfx_play_flag)
	{
		exspi_msc.cur_num=sfx_song_table[sys_cb.sfx_index-1];
		set_light_mode=sfx_light_mode_table[sys_cb.sfx_index-1];
		printf("sfx cur_num:%d\n",exspi_msc.cur_num);
	}
	else if(exspi_msc.song_int_flag&&exspi_msc.song_mode==1)
	{
		exspi_msc.cur_num=int_song_table[song_index];
		set_light_mode=int_light_table[song_index];
	}
	else
	{
		exspi_msc.cur_num= song_mode_table[exspi_msc.song_mode][song_index];
		set_light_mode=light_mode_table[exspi_msc.song_mode][song_index];
		if(exspi_msc.cur_num==6)
		{
			set_light_mode=song1_time[0].light_mode;
			exspi_msc.time_offset=1;
		}
		else if(exspi_msc.cur_num==7)
		{
			set_light_mode=song2_time[0].light_mode;
			exspi_msc.time_offset=1;
		}
	}
	exspi_msc.pause=false;
	play_time=0;
	song_ticks=0;
	amp_speaker_unmute();	
	if(exspi_msc.sfx_play_flag)
	{
		bsp_set_sfx_mode(set_light_mode);
	}
	else
	{
		if(exspi_msc.mic_play_flag||exspi_msc.sfx_play_flag||exspi_msc.song_int_flag)
		{
			bsp_set_light_mode(set_light_mode);
		}
		else 
		{
			if(sys_cb.sfx_index!=0)
			{
				bsp_set_sfx_mode(sfx_light_mode_table[sys_cb.sfx_index-1]);
			}
			else
			{
				bsp_set_light_mode(set_light_mode);
			}
		}		
	}
	exspifalsh_music_num_kick(exspi_msc.cur_num);
}
AT(.text.func.music)
void func_check_song_play(void)
{
	if (get_music_dec_sta() == MUSIC_STOP)
	{
		u16 delay_time;
		if(exspi_msc.pause)
		{
			return;
		}
		if(exspi_msc.mic_play_flag)
		{
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			exspi_msc.mic_play_flag=false;
			if(sys_cb.sfx_index!=0)
			{
				bsp_set_sfx_mode(sfx_light_mode_table[sys_cb.sfx_index-1]);
			}
			else
			{
				bsp_set_light_mode(LIGHT_SEQUENCE_2);
			}
			return;
		}
		if(exspi_msc.sfx_play_flag)
		{
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			exspi_msc.sfx_play_flag=false;
			if(sys_cb.sfx_index!=0)
			{
				bsp_set_sfx_mode(sfx_light_mode_table[sys_cb.sfx_index-1]);
			}
			else
			{
				bsp_set_light_mode(LIGHT_SEQUENCE_2);
			}
			return;
		}
		if(exspi_msc.song_int_flag)
		{
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			if(sys_cb.sfx_index!=0)
			{
				bsp_set_sfx_mode(sfx_light_mode_table[sys_cb.sfx_index-1]);
			}
			else
			{
				bsp_set_light_mode(LIGHT_SEQUENCE_2);
			}
			return;
		}
		if(exspi_msc.song_mode==3)
		{
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			//if(func_cb.last == FUNC_BT)
			if(sys_cb.bt_connected)
			{
				music_control(MUSIC_MSG_STOP);
				func_cb.sta = FUNC_BT;
			}
			else
			{
				if(sys_cb.sfx_index!=0)
				{
					bsp_set_sfx_mode(sfx_light_mode_table[sys_cb.sfx_index-1]);
				}
				else
				{
					bsp_set_light_mode(LIGHT_SEQUENCE_2);
				}
			}
			return;
		}
		u8 song_index=exspi_msc.song_info[exspi_msc.song_mode].song_index;

		delay_time=delay_info_table[exspi_msc.song_mode][song_index];
		if(delay_time!=0)
		{

			if(tick_check_expire(ticks_1,delay_time))
			{
				ticks_1=tick_get();
				if(!tick_start)
				{
					printf("tick start:%d\n",delay_time);
					tick_start=1;
					return;
				}
			}
			else
			{
				return;
			}
			tick_start=0;
			printf("tick end!!\n");
		}
		switch(exspi_msc.song_mode)
		{
			case 0:
				if(exspi_msc.song_info[0].song_index==3)
				{
					exspi_msc.song_info[0].song_index++;
				}
				else
				{
					exspi_msc.song_info[0].song_index++;
					if(exspi_msc.song_info[0].song_index>=max_song_table[0])
					{
						exspi_msc.song_info[0].song_index=0;
					}
					exspi_msc.song_mode=1;
				}
				break;
			case 1:
				exspi_msc.song_info[1].song_index++;
				if(exspi_msc.song_info[1].song_index>=max_song_table[1])
				{
					exspi_msc.song_info[1].song_index=0;
				}
				#if FUNC_NO_SPEECH
				exspi_msc.song_mode=3;
				return;
				#else
				exspi_msc.song_mode=2;
				#endif
				break;
			case 2:
				if(exspi_msc.song_info[2].song_index==2)
				{
					exspi_msc.song_info[2].song_index++;
				}
				else
				{
					exspi_msc.song_info[2].song_index++;
					if(exspi_msc.song_info[2].song_index>=max_song_table[2])
					{
						exspi_msc.song_info[2].song_index=0;
					}
					exspi_msc.song_mode=1;
					exspi_msc.song_mode=3;
					return;
				}
				break;
		}
		set_song_play_num();

	}
}


AT(.text.func.music)
void func_song_light_sync_deal(void)
{

	u16 song_play_time=0;
	u8 light_mode=0;
	if(sys_cb.cur_mode!=HOME_MODE)
	{
		return;
	}

	if(exspi_msc.cur_num!=6&&exspi_msc.cur_num!=7)
	{
		return;
	}
	
	if(tick_check_expire(song_ticks,10))
	{
		song_ticks = tick_get();
		//printf("cur_num:%d\n",exspi_msc.cur_num);
		if(exspi_msc.cur_num==6)
		{
			#if 0
			msc_breakpiont_t song_brkpt;
			u32 cur_time1 = music_get_cur_time();
			music_get_breakpiont(&song_brkpt);			
			printf("time:%d->frame_count:0x%x->file_ptr:0x%x\n",cur_time1,song_brkpt.frame_count,song_brkpt.file_ptr);
			#endif
			song_play_time=song1_time[exspi_msc.time_offset].time;
			light_mode=song1_time[exspi_msc.time_offset].light_mode;
		}
		else if(exspi_msc.cur_num==7)
		{
			
			song_play_time=song2_time[exspi_msc.time_offset].time;
			light_mode=song2_time[exspi_msc.time_offset].light_mode;
			
		}
		if(play_time==song_play_time)
		{

			u32 cur_time1 = music_get_cur_time();
			if(play_time!=(cur_time1/10))
			{
				play_time=cur_time1*10;
			}
			if(sys_cb.sfx_index!=0)
			{
				bakup_light_mode=light_mode;
			}
			else
			{
				bsp_set_light_mode(light_mode);
			}
			
			exspi_msc.time_offset++;
		}
		play_time++;
	}
}

AT(.text.func.music)
void func_exspifalsh_music_process(void)
{
    func_process();
	func_check_song_play();
	func_song_light_sync_deal();
}


void func_switch_song(void)
{
	printf("song_mode:%d\n",exspi_msc.song_mode);
	music_control(MUSIC_MSG_STOP);
	switch(exspi_msc.song_mode)
	{
		case 0:
			exspi_msc.song_info[0].song_index++;
			if(exspi_msc.song_info[0].song_index>=(max_song_table[0]-1))
			{
				exspi_msc.song_info[0].song_index=0;
			}
			exspi_msc.song_mode=1;
			exspi_msc.song_int_flag=0;
		break;
		case 1:
			if(exspi_msc.song_int_flag==0)
			{
				exspi_msc.song_int_flag=1;
			}
			else
			{
				exspi_msc.song_int_flag=0;
				exspi_msc.song_info[1].song_index++;
				if(exspi_msc.song_info[1].song_index>=max_song_table[1])
				{
					exspi_msc.song_info[1].song_index=0;
				}
				#if !FUNC_NO_SPEECH
				exspi_msc.song_mode=0;
				#endif
			}
		break;
		case 2:
			exspi_msc.song_mode=0;
			exspi_msc.song_int_flag=0;
		    exspi_msc.song_info[2].song_index++;
			if(exspi_msc.song_info[2].song_index==2)
			{
				exspi_msc.song_info[2].song_index=4;
			}
			if(exspi_msc.song_info[2].song_index>=max_song_table[2])
			{
				exspi_msc.song_info[2].song_index=0;
			}
		break;
		case 3:
			#if FUNC_NO_SPEECH
			exspi_msc.song_mode=1;
			#else
			exspi_msc.song_mode=0;
			#endif
			break;
		default:
		break;
	}


	ticks_1=0;
	exspi_msc.sfx_play_flag=0;
	exspi_msc.mic_play_flag=0;
	set_song_play_num();
}
AT(.text.func.music)
void exspifalsh_music_switch_file(u8 direction)   //1 next , 0 prev
{
    music_control(MUSIC_MSG_STOP);
    printf("direction = %d, music_num = %d, total = %d\n",direction, exspi_msc.cur_num,exspi_msc.total_num);
	func_switch_song();

}


AT(.text.func.music)
bool exspifalsh_music_image_check(void)
{
    if (is_exspiflash_online()) {
        exspi_msc.total_num = exspifalsh_music_get_file_total();
		printf("exspi_msc.total_num = %d\n",exspi_msc.total_num);
		#if 0
		if(!exspi_msc.project_num&&exspi_msc.total_num>1)
		{
			exspi_msc.total_num=exspi_msc.total_num/2;
			printf("project total num= %d\n",exspi_msc.total_num);
		}
        #endif
        if (exspi_msc.total_num > 0) {
            return true;
        }
    }
    return false;
}

AT(.text.func.music)
void func_exspifalsh_mp3_res_play(u32 addr, u32 len)
{
    msc_breakpiont_t brkpt;
    music_get_breakpiont(&brkpt);           //保存当前播放位置
    u32 cur_time = music_get_cur_time();
    music_control(MUSIC_MSG_STOP);
    register_spi_read_function(NULL);       //恢复内部SPI读接口.
    mp3_res_play(addr, len);
    register_spi_read_function(spiflash1_read);
    music_set_cur_time(cur_time);
    exspifalsh_music_num_kick(exspi_msc.cur_num);
    music_set_jump(&brkpt);                 //恢复播放位置
}

AT(.text.func.music)
void func_check_timeout(void)
{
	if(exspi_msc.pause)
	{
		exspi_msc.timeout_cnt++;
		amp_speaker_mute();
		if(exspi_msc.timeout_cnt>=exspi_msc.max_timeout_cnt)
		{
			exspi_msc.timeout_cnt=0;
			exspi_msc.song_int_flag=0;
			music_control(MUSIC_MSG_STOP);
			func_cb.sta = FUNC_PWROFF;
		}
	}
}
AT(.text.func.music)
void func_mic_onoff_ctl(void)
{
	printf("mic_onoff:%d\n",sys_cb.mic_onoff);
	#if SYS_KARAOK_EN
	if(sys_cb.mic_onoff)
	{
		sys_cb.mic_onoff=0;
		exspi_msc.max_timeout_cnt=MAX_MICOFF_TIMEOUT_TIME;
		if(exspi_msc.pause)
		{
			exspi_msc.mic_play_flag=true;
			set_song_play_num();
		}
		//karaok_mic_mute();
		
		bsp_karaok_exit(AUDIO_PATH_KARAOK);
	}
	else
	{
		if(sys_cb.vbat_low)
		{
			return;
		}
		sys_cb.mic_onoff=1;
		exspi_msc.max_timeout_cnt=MAX_MICON_TIMEOUT_TIME;
		if(exspi_msc.pause)
		{
			exspi_msc.mic_play_flag=true;
			set_song_play_num();
		}
		//karaok_mic_unmute();
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		//bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
	}
	#endif
}
AT(.text.func.music)
void func_sfx_ctl(void)
{
	printf("sfx_index:%d\n",sys_cb.sfx_index);
	sys_cb.sfx_index++;
	if(sys_cb.sfx_index>9)
	{
		if((exspi_msc.pause)||exspi_msc.song_mode!=1)
		{
			sys_cb.sfx_index=1;
		}
		else
		{
			sys_cb.sfx_index=0;
		}

	}
	if(exspi_msc.song_mode!=1||exspi_msc.pause)
	{
		exspi_msc.sfx_play_flag=true;
		set_song_play_num();
	}
	else
	{
		if(sys_cb.sfx_index==0)
		{
			bsp_set_light_mode(bakup_light_mode);
		}
		else
		{
			if(exspi_msc.sfx_play_flag)
			{
				music_control(MUSIC_MSG_STOP);
				set_song_play_num();
			}
			else
			{
				bsp_set_sfx_mode(sfx_light_mode_table[sys_cb.sfx_index-1]);
			}

		}

	}

}
AT(.text.func.music)
void func_idlde_sfx_ctl(void)
{
	printf("sfx_index:%d\n",sys_cb.sfx_index);
	sys_cb.sfx_index++;
	if(sys_cb.sfx_index>9)
	{
		if(exspi_msc.pause)
		{
			sys_cb.sfx_index=1;
		}
		else
		{
			sys_cb.sfx_index=0;
		}

	}
	if(exspi_msc.pause)
	{
		exspi_msc.sfx_play_flag=true;
		set_song_play_num();
	}
	else
	{
		if(sys_cb.sfx_index==0)
		{
			bsp_set_sfx_mode(LIGHT_SEQUENCE_5);
		}
		else
		{
			if(exspi_msc.sfx_play_flag)
			{
				music_control(MUSIC_MSG_STOP);
				set_song_play_num();
			}
			else
			{
				bsp_set_sfx_mode(sfx_light_mode_table[sys_cb.sfx_index-1]);
			}

		}

	}

}
AT(.text.func.music)
void func_lower_power_check(void)
{
	#if SYS_KARAOK_EN
	u8 mic_off;
	if(sys_cb.mic_onoff&&sys_cb.karaok_init)
	 {
	 	u8 mic_off;
	 	mic_off=bsp_get_mic_vol_offset();
		if(sys_cb.mic_vol_offset!=mic_off)
		{
			sys_cb.mic_vol_offset=mic_off;
			sys_cb.music_vol_offset=mic_off;
			bsp_karaok_set_mic_volume();
			bsp_change_volume(MUSIC_VOL-mic_off);
		}
		if ((sys_cb.vbat <=2700) ||(adc_cb.vrtc_val>=0x39f))
		 {
		 	sys_cb.vbat_low=1;
			sys_cb.mic_onoff=0;
			exspi_msc.max_timeout_cnt=MAX_MICOFF_TIMEOUT_TIME;
			bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 }			 	
	 }
	 else 
	{
		mic_off=bsp_get_mic_vol_offset();
		//printf("vol_offset:%d->offset:%d\n",mic_off,sys_cb.music_vol_offset);

		if(sys_cb.music_vol_offset!=mic_off)
		{
			sys_cb.music_vol_offset=mic_off;
			bsp_change_volume(MUSIC_VOL-mic_off-1);
		}
		
	}
	 #endif
}
AT(.text.func.music)
void func_set_sfx_off(void)
{
	sys_cb.sfx_index=9;
	bsp_set_sfx_mode(sfx_light_mode_table[sys_cb.sfx_index-1]);
}

void bakup_song_info(void)
{
	if(exspi_msc.song_mode==1)
	{
		exspi_msc.song_info[1].song_index++;
		if(exspi_msc.song_info[1].song_index>=max_song_table[1])
		{
			exspi_msc.song_info[1].song_index=0;
		}
		exspi_msc.song_int_flag=0;
		sys_cb.song_num=exspi_msc.song_info[1].song_index;
		param_song_index_write();
	}
	exspi_msc.song_mode=0;
	sys_cb.song_mode=exspi_msc.song_mode;
	memcpy(sys_cb.song_index,exspi_msc.song_info,sizeof(song_info_t)*3);
	printf("bakup info:%d\n",sys_cb.song_mode);
	print_r(sys_cb.song_index,3);
}
void restore_song_info(void)
{
	exspi_msc.song_mode=sys_cb.song_mode;
	memcpy(exspi_msc.song_info,sys_cb.song_index,sizeof(song_info_t)*3);
	printf("restor info:%d\n",exspi_msc.song_mode);
	print_r(exspi_msc.song_info,3);
}

AT(.text.func.music)
void func_song_info_init(void)
{
	printf("first_into_song:%d\n",sys_cb.first_into_song);
	if(sys_cb.first_into_song)
	{
		sys_cb.first_into_song=0;
		memset(&exspi_msc,0x0,sizeof(exspifalsh_msc_t));
		memset(sys_cb.song_index,0x0,sizeof(song_info_t)*3);
	}
	else
	{

		restore_song_info();
	}
	param_song_index_read();
	exspi_msc.song_info[1].song_index=sys_cb.song_num;
}

AT(.text.func.music)
static void func_exspifalsh_music_enter(void)
{
    msg_queue_clear();
	func_song_info_init();
	exspiflash_init();
	if ((!exspifalsh_music_image_check())){
        func_cb.sta = FUNC_BT;
        return;
    }
	func_cb.mp3_res_play = NULL;//func_exspifalsh_mp3_res_play;
	
    register_spi_read_function(spiflash1_read);

	printf("enter song mode:%d\n",exspi_msc.song_mode);
	exspi_msc.timeout_cnt=0;
	bsp_change_volume(0);
	#if SYS_KARAOK_EN
	if(sys_cb.mic_onoff)
	{	
		//bsp_karaok_exit(AUDIO_PATH_KARAOK);
		music_control(MUSIC_MSG_STOP);
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		delay_ms(10);
		exspi_msc.max_timeout_cnt=MAX_MICON_TIMEOUT_TIME;
	}
	else
	{
		exspi_msc.max_timeout_cnt=MAX_MICOFF_TIMEOUT_TIME;
	}

	#endif
	bsp_set_volume(MUSIC_VOL);
	#if FUNC_NO_SPEECH
	exspi_msc.song_mode=1;
	#endif
	set_song_play_num();
}

AT(.text.func.music)
static void func_exspifalsh_music_exit(void)
{
    //music_control(MUSIC_MSG_STOP);
    printf("music exit!!\n");
    if (get_music_dec_sta() == MUSIC_PLAYING)
    {
    	printf("music plaging\n");
    	music_control(MUSIC_MSG_STOP);
    }
	bakup_song_info();
	sys_cb.exspiflash_music_mode=0;
	sys_cb.bt_connected=0;
	#if SYS_KARAOK_EN
	if(sys_cb.mic_onoff)
	{
		 bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 delay_ms(30);
	}
	#endif
    register_spi_read_function(NULL);  //恢复内部SPI读接口.
    func_cb.last = FUNC_EXSPIFLASH_MUSIC;
}

AT(.text.func.music)
void func_exspifalsh_music(void)
{
    printf("%s\n", __func__);
	//exspi_msc.project_num=param_spiflash_project_num_read();
    func_exspifalsh_music_enter();
    while (func_cb.sta == FUNC_EXSPIFLASH_MUSIC) {
        func_exspifalsh_music_process();
        func_exspifalsh_music_message(msg_dequeue());
        //func_exspifalsh_music_display();
    }
    func_exspifalsh_music_exit();
}
#endif

